package com.example.musictest.ui.notifications;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.musictest.R;
import com.example.musictest.databinding.FragmentNotificationsBinding;
import com.example.musictest.ui.adapter.PictureAdapter;
import com.example.musictest.ui.home.HomeViewModel;
import com.zy.base.fragment.BaseFragment;
import com.zy.untils.MyLog;

import java.util.ArrayList;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class NotificationsFragment extends BaseFragment {

    private NotificationsViewModel notificationsViewModel;
    private FragmentNotificationsBinding binding;
    private HomeViewModel homeViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        notificationsViewModel =
                new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(NotificationsViewModel.class);
        binding= DataBindingUtil.inflate(inflater, R.layout.fragment_notifications, container, false);
        View root = binding.getRoot();
        homeViewModel = new ViewModelProvider(requireActivity()).get(HomeViewModel.class);
        initView();
        return root;
    }
    private FragmentActivity context;
    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        this.context=requireActivity();
    }
    private void initView() {
        PictureAdapter adapter = new PictureAdapter(requireActivity(),new ArrayList<>(), R.layout.item_video_recy);
        LinearLayoutManager layoutManager = new LinearLayoutManager(requireActivity());
        binding.pictureRecyclerview.setLayoutManager(layoutManager);
        binding.pictureRecyclerview.setAdapter(adapter);

        homeViewModel.pictures.observe(context, pictures -> {
            MyLog.i("数据：pictures"+pictures.size()) ;
            adapter.updateData(pictures);
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}